package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = "1.0.0", interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public Result findPAge(QueryPageBean queryPageBean) {
//        System.out.println(1/0);
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckItem> checkItemPage = checkItemDao.findByCondition(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult();
        pageResult.setRows(checkItemPage.getResult());
        pageResult.setTotal(checkItemPage.getTotal());
        return Result.success(pageResult);
    }

    @Override
    public Result add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
        return Result.success(null);
    }

    @Override
    public Result update(CheckItem checkItem) {
        checkItemDao.update(checkItem);
        return Result.success(null);
    }

    @Override
    public Result delete(Integer id) {
        long count=checkItemDao.countCheckGroupAndItemById(id);
        if(count>0){
          return Result.fail(-999,"已经被使用，无法进行删除操作");
        }else{
            checkItemDao.delete(id);
            return Result.success(null);
        }
    }

    @Override
    public long findByName(String name) {
        return checkItemDao.findByName(name);
    }

    @Override
    public long findByCode(String code) {
        return checkItemDao.findByCode(code);
    }
}
